import sys, os
from datetime import datetime
import pickle

from monte.gym import trainer #everything to do with training resides in
                              #monte.gym

PATH = (os.path.abspath(os.path.dirname(os.path.realpath(__file__))))

def print_help():
    print "Usage: %s <PROFILE_NAME> [CRF_MODEL_FILE] [ABSTRACTS_DIRECTORY]" % sys.argv[0].split('/')[-1]

profile = "DEFAULT"
if len(sys.argv) > 1:
    profile = sys.argv[1]
    if profile.find('-h') != -1:
        print_help()
        sys.exit(0)


STARTTIME = datetime.now()
OUTPUT_PATH = os.path.join(PATH, "outputs", "%s_%s_%s" % (
                            sys.argv[0].split('/')[-1], 
                            profile, 
                            STARTTIME.strftime("%F_%H-%M-%S")))
os.mkdir(OUTPUT_PATH)
print >> sys.stderr, "Find the output at: %s" % OUTPUT_PATH
OUT = open("%s/output.txt" % OUTPUT_PATH, 'w')

CRF_FILE = "%s/crf_model.obj" % OUTPUT_PATH
if len(sys.argv) > 2:
    CRF_FILE = sys.argv[2]

TRAIN_PATH = os.path.join(PATH, "../data/bionlp09_shared_task_training_data")
if len(sys.argv) > 3:
    TRAIN_PATH = sys.argv[3]

# PROFILES ######################################################

DEFAULT = {
    "SAMPLE_SIZE": 50,
    "TRAIN_STEPS": 35,
    "RADIUS": 8,
    "TRAINER": trainer.Conjugategradients,
    "TRAINER_PARAMS": (10, ),
    "TEST": False,
    "FSCORE": True,
    "OUT": OUT,
    "PATH": PATH,
    "TRAIN_DIR": TRAIN_PATH,
    "OUTPUT_PATH": OUTPUT_PATH,
    "CRF": None,
    "CRF_FILE": CRF_FILE,
    "DO_TRAIN": True,
    "MAKE_ARCHIVE": False,
    "USE_GOLD_TRIGGERS": True,
    "METHOD_FLAGS": (
        (1, 4, ), # Gene_expression
        (2, 0, ), # Localization
        (1, 1, ), # Transcription
        (1, 1, ), # Binding
        (2, 0, ), # Phosphorylation
        (5, 0, ), # Positive_regulation
        (5, 0, ), # Regulation
        (2, 0, ), # Protein_catabolism
        (5, 0, ), # Negative_regulation
    ),
    "MOZAKHRAF": ('whereas', 'and', 'or', 'but')
}

LARGE = DEFAULT.copy()
LARGE.update({
    "SAMPLE_SIZE": 800,
    "TRAIN_STEPS": 10,
})

MEDIUM = DEFAULT.copy()
MEDIUM.update({
    "SAMPLE_SIZE": 400,
    "TRAIN_STEPS": 10,
})

SMALL = DEFAULT.copy()
SMALL.update({
    "SAMPLE_SIZE": 100,
    "TRAIN_STEPS": 10,
})

DEVEL = DEFAULT.copy()
DEVEL.update({
    "SAMPLE_SIZE": 150,
    "TRAIN_STEPS": 5,
})

LAST_TRY = DEVEL.copy()
LAST_TRY.update({
    "TRAIN_STEPS": 2,
})

TEST = DEFAULT.copy()
TEST.update({
    "DO_TRAIN": False,
    "SAMPLE_SIZE": 0,
    "MAKE_ARCHIVE": "CRF",
})

TEST2 = TEST.copy()
TEST2.update({
    "SAMPLE_SIZE": 2,
})


FINAL1 = TEST.copy()
FINAL1.update({
    "TRAIN_DIR": os.path.join(PATH, "../data/bionlp09_shared_task_test_data_without_gold_annotation"),
    "FSCORE": False,
    "METHOD_FLAGS": (
        (1, 4, ), # Gene_expression
        (2, 0, ), # Localization
        (2, 0, ), # Transcription
        (4, 2, 1), # Binding
        (2, 0, ), # Phosphorylation
        (5, 0, ), # Positive_regulation
        (5, 0, ), # Regulation
        (2, 0, ), # Protein_catabolism
        (5, 0, ), # Negative_regulation
    ),
})

FINAL2 = TEST.copy()
FINAL2.update({
    "TRAIN_DIR": os.path.join(PATH, "../data/bionlp09_shared_task_test_data_without_gold_annotation"),
    "FSCORE": False,
})

PAPER = TEST.copy()
PAPER.update({
    "TRAIN_DIR": os.path.join(PATH, "sample-100"),
    "FSCORE": False,
})

FARZIN = TEST.copy()
FARZIN.update({
    "TRAIN_DIR": os.path.join(PATH, "farzin"),
    "FSCORE": False,
})


#################################################################
'''
try:
    CRF_FILE = globals()[profile]["CRF_FILE"]
except:
    print_help()
    sys.exit(-1)


if os.path.exists(CRF_FILE):
    print >> OUT, ". Loading the CRF ............................."
    crf_f = open(CRF_FILE, 'r')
    globals()[profile]["CRF"] = pickle.load(crf_f)
    crf_f.close()

'''
